﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Data;
public partial class admin_ModifyVA : System.Web.UI.Page
{
	int component_id = 0;
	protected void Page_Load(object sender, EventArgs e)
	{
		component_id = Int32.Parse((String)Request.QueryString["ComponentID"]);
		String componentname = (String)Request.QueryString["componentname"];
		String machinename = (String)Request.QueryString["machinename"];
		comp_label.Text = machinename + "--->" + componentname;
		Session["compID"] = component_id;
		if (!IsPostBack)
		{
			LoadReports();

		}
	}
	protected void GridViewVA_OnRowCreated(object sender, GridViewRowEventArgs e)
	{
		GridViewVA.DataKeyNames = new[] { "component_id", "dateRecorded" };
	}
	protected void LoadReports()
	{
		component_id = Int32.Parse(Session["compID"].ToString());
		string query_reports = "select component_id,convert(varchar(10),dateRecorded,103) AS dateRecorded,status_id,comments From ComponentStatus cs"
			+ " where cs.component_id='" + component_id + "'";
		try
		{
			CreateConn objcon = new CreateConn();
			DataTable dt = objcon.SQLConnDataTable(query_reports);
			if (dt != null && dt.Rows.Count > 0)
			{
				dt.Columns.Add("severity");
				foreach (DataRow dr in dt.Rows)
				{
					string status = dr["status_id"].ToString();
					dr.BeginEdit();
					try
					{
						dr["severity"] = this.getsev(Int32.Parse(status));//change the value
					}
					catch (Exception e)
					{
						Response.Write("Err:" + e.ToString());
					}
					dr.EndEdit();
				}
				Session["dt"] = dt;
				GridViewVA.DataSource = dt;
				GridViewVA.DataBind();
			}
		}
		catch (Exception e)
		{
		}

	}

	protected void GridViewVA_OnRowEditing(object sender, GridViewEditEventArgs e)
	{
		GridViewVA.EditIndex = e.NewEditIndex;
		LoadReports();
	}
	protected void GridViewVA_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		string dateRecorded = GridViewVA.DataKeys[e.RowIndex]["dateRecorded"].ToString();
		int componentID = Int32.Parse(GridViewVA.DataKeys[e.RowIndex]["component_id"].ToString());
		string delete_query = "delete from ComponentStatus where component_id='" + componentID + "' and "
			+ " dateRecorded = convert(datetime,'" + dateRecorded + "',103)";
		CreateConn objcon = new CreateConn();
		try
		{
			objcon.SQLConnDataTable(delete_query);
			LoadReports();
		}
		catch (Exception e2)
		{
			Response.Write(e2.Message);
		}
	}
	protected void GridViewVA_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		GridViewVA.EditIndex = -1;
		LoadReports();
	}

	protected void GridViewVA_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		GridViewRow row = GridViewVA.Rows[e.RowIndex];
		DropDownList severity = (DropDownList)row.Cells[0].FindControl("severity_dropdownlist");
		int status_id = Int32.Parse(severity.SelectedValue);
		string comments = ((TextBox)(row.Cells[1].Controls[1])).Text;
		string new_dateReco = ((TextBox)(row.Cells[2].Controls[1])).Text;
		int count = row.Cells.Count;
		string dateRecorded = GridViewVA.DataKeys[e.RowIndex]["dateRecorded"].ToString();
		int componentID = Int32.Parse(GridViewVA.DataKeys[e.RowIndex]["component_id"].ToString());
		string query = "Update ComponentStatus set comments='" + comments + "',status_id='"
		  + status_id + "',dateRecorded=CONVERT(datetime,'" + new_dateReco + "',103) WHERE "
		  + "dateRecorded = CONVERT(datetime,'" + dateRecorded + "',103) and component_id='" + componentID + "'";
		CreateConn objcon = new CreateConn();
		objcon.SQLConnDataTable(query);
		GridViewVA.EditIndex = -1;
		LoadReports();
		GridViewVA.DataBind();
	}
	protected string getsev(int statusid)
	{
		string str;
		switch (statusid)
		{
			case 1: str = "NORMAL"; break;
			case 2: str = "ABNORMAL"; break;
			case 3: str = "Moderate"; break;
			case 4: str = "Severe"; break;
			default: str = ""; break;
		}
		return str;
	}
	protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		GridViewVA.PageIndex = e.NewPageIndex;
		LoadReports();
		GridViewVA.DataBind();
	}
}
